package yl.hs.bmipfull.service.salemanage.impl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import yl.hs.bmipfull.daos.salemanage.IFollowupRecordDao;
import yl.hs.bmipfull.pojo.salemanage.CustomerInfoEntity;
import yl.hs.bmipfull.pojo.salemanage.FollowupRecordEntity;
import yl.hs.bmipfull.pojo.system.UserInfoEntity;
import yl.hs.bmipfull.service.salemanage.IFollowupRecordService;
import yl.hs.bmipfull.utils.DataState;
import yl.hs.bmipfull.utils.HsApp;
import yl.hs.bmipfull.utils.SearchFilterEngine;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.utils.datastate.ActionType;
import yl.hs.bmipfull.utils.datastate.RelationType;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.PageResult;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

/**
 * @author 谢东
 * @apiNote 跟进记录服务层接口实现
 * @since 2022年08月03日 09时32分39秒 <br/>
 */
@Service
public class FollowupRecordServiceImpl extends SaleBaseService implements IFollowupRecordService {

    @Autowired
    IFollowupRecordDao dao;

    @Override
    public List<FollowupRecordEntity> queryList(PageQuery pageQuery) throws Exception {
        pageQuery.checkSortSides(FollowupRecordEntity.class);
        var filters = SearchFilterEngine.generateQueryList(pageQuery.getFilters(), FollowupRecordEntity.class);
        return dao.queryPage(pageQuery.getQuery(),getTagSNs(),filters);
    }

    @Override
    public PageResult<FollowupRecordEntity> queryPage(PageQuery pageQuery, int rowNum, int page) throws Exception {
        pageQuery.checkSortSides(FollowupRecordEntity.class);
        var filters = SearchFilterEngine.generateQueryList(pageQuery.getFilters(), FollowupRecordEntity.class);
        pageQuery.getQuery().put("querySkip", (page - 1) * rowNum);
        pageQuery.getQuery().put("pageSize", rowNum);
        PageResult<FollowupRecordEntity> result = new PageResult(rowNum);
        result.setRecords(dao.queryCountByCondition(pageQuery.getQuery(),getTagSNs(),filters));
        result.setPage(page);
        result.setData(dao.queryPage(pageQuery.getQuery(),getTagSNs(),filters));
        return result;
    }


    @Override
    public FollowupRecordEntity findBySn(String sn) {
        return dao.findBySN(sn);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer saveFollowupRecord(Map<String, Object> model, String sn) throws Exception {
        Integer result = 0;
        FollowupRecordEntity entity = null;
        if (Tl.isEmpty(sn)) {
            //执行保存
            entity = new FollowupRecordEntity();
            Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            entity.setSn(queryNewSN(FollowupRecordEntity.class));
            SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
            entity.setName(format.format(entity.getFollowDate())+entity.getUserName()+"拜访"+entity.getCustomerName());
            result = dao.insertNew(entity);
            var shareCode = writeUserTag(entity.getSn(),entity.getClass(), RelationType.CREATE.toString());
            saveGS(
                    entity.getSn(),
                    entity.getName(),
                    String.join(",",
                            entity.getName(),
                            entity.getAddress()
                    ),
                    entity,
                    HsApp.getCurrent().getUserSN(),
                    shareCode
            );
            if (result > 0) {
                writeLog(ActionType.CREATE, entity.getSn(), "插入跟进记录信息", Tl.toJson(entity), FollowupRecordEntity.class);
            }

        } else {
            //执行修改
            entity = findBySn(sn);
            Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            result = dao.update(entity);
            saveGS(
                    entity.getSn(),
                    entity.getName(),
                    String.join(",",
                            entity.getName(),
                            entity.getAddress()
                    ),
                    entity,
                    HsApp.getCurrent().getUserSN(),
                    null
            );
            if (result > 0) {
                writeLog(ActionType.UPDATE, sn, "更新跟进记录信息", Tl.toJson(entity), FollowupRecordEntity.class);
            }
        }

        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer delete(String[] sn) {
        Integer result = dao.changeStateBySN(sn, DataState.DELETED.code);
        if (result > 0) {
            writeLog(ActionType.DELETE, sn, FollowupRecordEntity.class);
        }
        return result;
    }
}